/**
 * Copyright [2016-2017] [yadong.zhang]
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this video except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ysstech.common.entity;

import java.io.Serializable;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import lombok.Data;

@Data
public class PageBean<T> implements Serializable {
    private long total;        //总记录数
    private List<T> rows;    //结果集
    private long pageNum;    // 第几页
    private long pageSize = 10;    // 每页记录数
    private long size;        // 当前页的数量 <= pageSize，该属性来自ArrayList的size属性


    public PageBean(IPage page) {
        this.total = page.getTotal();
        this.rows = page.getRecords();
        this.pageNum = page.getPages();
        this.pageSize = page.getSize();
        this.size = page.getCurrent();
    }

    /**
     * 包装Page对象，因为直接返回Page对象，在JSON处理以及其他情况下会被当成List来处理，
     * 而出现一些问题。
     *
     * @param list page结果
     */
    public PageBean(List<T> list, int total) {
        if (list instanceof Page) {
            Page<T> page = (Page<T>) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = total;
            this.rows = page;
            this.size = page.size();
        }
    }
}
